package com.epro.dx.widget;

import com.epro.dx.R;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * @author Tony
 * 描述：显示倒计时的功能控件
 */
public class TimerView extends LinearLayout {

	private Context mContext;
	private TextView h;//时
	private TextView m;//分
	private TextView s;//秒
	private int hh,mm,ss;
	private boolean isFirstTime = true;
	private boolean isStoped = false;
	public TimerView(Context context) {
		this(context,null);
		// TODO Auto-generated constructor stub
	}
	
	public TimerView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.mContext = context;
		setWillNotDraw(false);
		initView();
		// TODO Auto-generated constructor stub
	}
	
	/*public TimerView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.mContext = context;
		setWillNotDraw(false);
		initView();
	}*/

	/**
	 * 加载视图
	 */
	private void initView() {
		LayoutInflater inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.widget_timer, this);
		
		h = (TextView) findViewById(R.id.timer_h);
		m = (TextView) findViewById(R.id.timer_m);
		s = (TextView) findViewById(R.id.timer_s);
		
	}
	
	/**
	 * 设置倒计时启动时分秒
	 * @param h 小时
	 * @param m 分钟
	 * @param s 秒
	 */
	public void setTime(int  h,int m,int s){
		this.hh = h;
		this.mm = m;
		this.ss = s;
		if(h>=0&&m>=0&&s>=0){
			setH(h);
		 if(m<=59&&s<=59){
			setM(m);
			setS(s);
		 }
		}
	}

	Handler handler = new Handler();
	Runnable runnable = new Runnable(){

		@Override
		public void run() {
			// TODO Auto-generated method stub
			ss-=1;
			if(ss<0){
				mm-=1;
				ss = 59;
				if(mm<0){
					hh-=1;
					mm=59;
					setH(hh);
				}
				setM(mm);
			}
			setS(ss);
			runTimer();
		}
		
	};
	private OnTimeOutListener onTimeOutListener;
	
	public void setH(int h){
		if(h<10){
			this.h.setText("0"+h);
		}else{
			this.h.setText(""+h);
		}
	}
	public void setM(int m){
		if(m<10){
			this.m.setText("0"+m);
		}else{
			this.m.setText(""+m);
		}
	}
	public void setS(int s){
		if(s<10){
			this.s.setText("0"+s);
		}else{
			this.s.setText(""+s);
		}
	}
	
	/**
	 * 重启计时器
	 */
	public void reBootTimer(){
		if(isStoped){
			isFirstTime = true;
			isStoped = false;
			startTimer();
		}else{
			startTimer();
		}
	}
	
	/**
	 * 开启倒计时
	 */
	public void startTimer(){
		if(isFirstTime){
			runTimer();
		}
		isFirstTime = false;
	}
	
	/**
	 * 开始倒计时
	 */
	private void runTimer(){
//		if(isFirstTime)
//			currentThreadId = Thread.currentThread().getId();
//		isFirstTime = false;
		if(!(hh==0&&mm==0&&ss==0)){
//			if(Thread.currentThread().getId() != currentThreadId)
//				return;
			handler.postDelayed(runnable, 1000);
		}else{
			isStoped = true;
			if(this.onTimeOutListener!=null){
				this.onTimeOutListener.onTimeout();
			}
		}
	}

	public void setOnTimeOutListener(OnTimeOutListener onTimeOutListener){
		this.onTimeOutListener = onTimeOutListener;
	}
	
	/**
	 * @author Tony
	 * 计时时间到事件监听
	 */
	public interface OnTimeOutListener{
		void onTimeout();
	}

}
